package com.lottery.game.core.modules.service.app.v2.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lottery.game.common.base.service.impl.AbstractBaseServiceImpl;
import com.lottery.game.core.modules.entity.app.BetRecordVO;
import com.lottery.game.core.modules.entity.game.GameAnnouncement;
import com.lottery.game.core.modules.entity.game.GameGame;
import com.lottery.game.core.modules.entity.user.GameUserMoneyChange;
import com.lottery.game.core.modules.entity.user.GameUserMoneyVO;
import com.lottery.game.core.modules.entity.user.UpAndDownReport;
import com.lottery.game.core.modules.mapper.BetDao;
import com.lottery.game.core.modules.mapper.GameUserMoneyChangeDao;
import com.lottery.game.core.modules.mapper.UpAndDownReportDao;
import com.lottery.game.core.modules.mapper.v2.GameGameMapper;
import com.lottery.game.core.modules.service.app.v2.BetRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;

/**
 * @Description TODO
 * @Classname BetRecordServiceImpl
 * @Date 2019/4/24 14:44
 * @Author liusc <liushicheng1993@vip.qq.com>
 * @Version 1.0
 */
@Service
public class BetRecordServiceImpl extends AbstractBaseServiceImpl<GameAnnouncement> implements BetRecordService{

    @Autowired
    private BetDao betDao;
    @Autowired
    private UpAndDownReportDao upAndDownReportDao;
    @Autowired
    private GameUserMoneyChangeDao gameUserMoneyChangeDao;
    @Autowired
    private GameGameMapper gameGameMapper;


    @Override
    public PageInfo<BetRecordVO> findRecodeList(String id, Integer rows, Integer page, String
            endDate, String startDate,String betType,String recordStatus) {
        //分页
        PageHelper.startPage(page,rows);
        //查询
        PageInfo<BetRecordVO> recodeList = new PageInfo<>(betDao.findRecodeList(id,startDate,endDate,betType,recordStatus));
        return recodeList;
    }

    @Override
    public Map<String, Object> getBetSum(String userId, String startDate, String endDate) {

        return betDao.getBetSum(userId,startDate,endDate);
    }

    @Override
    public Object getPageInfo(String type, String id) {
        //充值详细
        if ("1".equals(type) ||  "2".equals(type)){
            UpAndDownReport upAndDownReport = upAndDownReportDao.get(id);
            return upAndDownReport;
            //撤单详细
        }else if("cancelBet".equals(type)){
            GameUserMoneyChange userMoneyChange = gameUserMoneyChangeDao.get(id);
            if (null != userMoneyChange){
                GameGame gameGame = gameGameMapper.selectByPrimaryKey(userMoneyChange.getGameId());
                userMoneyChange.setIssueApi(gameGame.getIssueApi());
            }
            return userMoneyChange;
            //投注详细
        }else {
            GameUserMoneyVO pageInfo = gameUserMoneyChangeDao.getPageInfo(id);
            if (null != pageInfo && !StringUtils.isEmpty(pageInfo.getBetStr())){
                pageInfo.setBetStrCH(getTempStrBet(pageInfo.getBetStr()));
            }
            if (null != pageInfo && !StringUtils.isEmpty(pageInfo.getNote()) || null != pageInfo && !StringUtils.isEmpty(pageInfo.getResultStr() )){
                pageInfo.setNoteStr(getTempStrNote(pageInfo.getNote(),pageInfo.getResultStr()));
            }
            if (StringUtils.isEmpty(pageInfo.getStatus())){
                pageInfo.setStatus("1");
            }
            return pageInfo;
        }
    }


    /**
     * @Description  note开奖转化为中文
     * @param: note
     * @Return
     * @Date 2019/5/21_14:04
     */
    private static String getTempStrNote(String pageInfoNote, String note) {

        if (StringUtils.isEmpty(note)){
            return "待开奖";
        }
        String[] arrayNote2 = note.split("\\|");
        StringBuffer tempStr2 = new StringBuffer();
        String substring="";
        //判断是否为空
        if (null != arrayNote2 && arrayNote2.length > 0 ) {
            for (int i = 0; i < arrayNote2.length; i++) {
                String tempStrBet = getTempStrBet(arrayNote2[i]);
                tempStr2.append(tempStrBet).append(",");
            }
            String s2 = tempStr2.toString();
            if (s2.endsWith(",")) {
                substring = s2.substring(0, s2.lastIndexOf(","));
            }
        }
        String[] arrayNote = pageInfoNote.split("\\|");
        StringBuffer tempStr = new StringBuffer();
        String resultStr = "";
        //判断是否为空
        if (null != arrayNote && arrayNote.length > 0 ){
            for (int i = 0; i < arrayNote.length-1; i++) {
                tempStr
                        .append(arrayNote[i])
                        .append("+");
            }
            String s = tempStr.toString();
            if (s.endsWith("+")) {
                resultStr = s.substring(0, s.lastIndexOf("+")) + "=" + substring;
            }
            return resultStr;
        }else {
            return "待开奖";
        }
    }

    /**
     * @Description 英文转中文，betStr
     * @param:
     * @Return
     * @Date 2019/5/12_13:53
     */
    private static String getTempStrBet(String bets) {
        String str = "";
        switch (bets){
            case "da":
                str = "大";
                break;
            case "xiao":
                str = "小";
                break;
            case "dan":
                str = "单";
                break;
            case "shuang":
                str = "双";
                break;
            case "dadan":
                str = "大单";
                break;
            case "dashuang":
                str = "大双";
                break;
            case "xiaodan":
                str = "小单";
                break;
            case "jida":
                str = "极大";
                break;
            case "jixiao":
                str = "极小";
                break;
            case "hongbo":
                str = "洪波";
                break;
            case "lanbo":
                str = "蓝波";
                break;
            case "lvbo":
                str = "绿波";
                break;
            case "baozi":
                str = "豹子";
                break;
            case "shunzi":
                str = "顺子";
                break;
            case "duizi":
                str = "对子";
                break;
            case "xiaoshuang":
                str = "小双";
                break;
            default:
                str = "";
        }
        return str;
    }


}
